set ns [new Simulator]

# Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

# Open the Trace file
set file1 [open network.attacks.traces w]
$ns trace-all $file1

# Open the NAM trace file
set file2 [open network.attacks.nam w]
$ns namtrace-all $file2

# Next line should be commented out to have the static routing
$ns rtproto DV

puts "Loading topology pattern..."

# Create network nodes
set numNodes 180
for {set i 0} {$i < $numNodes} {incr i} {
    set n($i) [$ns node]
}

set val(topology) 	"topology.tcl"         ;# Topology Pattern File Path
source $val(topology)

# Generating normal network traffic by traffic source generator
# from CMU's mobile code.
#
# usage: cbrgen.tcl [-type cbr|tcp] [-nn nodes]
#                   [-seed seed] [-mc connections] [-rate rate]

# Below the tcp-180 and udp-180 files were generated by:
#
# ns cbrgen.tcl -type tcp -nn 180 -seed 123 -mc 30
# ns cbrgen.tcl -type cbr -nn 180 -seed 321 -mc 30 -rate 0.25

set val(tcp) 	"tcp-180"         ;# Connection Pattern File Path
set val(udp) 	"udp-180"         ;# Connection Pattern File Path

puts "Loading traffic pattern..."
source $val(tcp)
source $val(udp)

puts "Loading attack scenario..."


#
# Setup UDP connections for attacks
#

# Define 50 attack_src on 5 attackers, 10 on each node
set nAttack_src 50
for {set i 0} {$i < $nAttack_src} {incr i} {
    set attack_src($i) [new Agent/UDP]
    # set network layer (IP) flow ID, to give them red color
    $attack_src($i) set fid_ 2
    
    if { $i < 10 } {
        $n(45) attach $attack_src($i) 911
    }
    if { $i >= 10 && $i < 20 } {
        $n(78) attach $attack_src($i) 911
    }
    if { $i >= 20 && $i < 30 } {
        $n(111) attach $attack_src($i) 911
    }
    if { $i >= 30 && $i < 40 } {
        $n(157) attach $attack_src($i) 911
    }
    if { $i >= 40} {
        $n(178) attach $attack_src($i) 911
    }
}

# Define 50 attack_dst on 10 nodes, 5 on each node
set nAttack_dst 50
for {set i 0} {$i < $nAttack_dst} {incr i} {
    set attack_dst($i) [new Agent/Null]
    $attack_dst($i) listen;
    
    if { $i < 5 } {
        $n(21) attach $attack_dst($i) 7
    }
    if { $i >= 5 && $i < 10 } {
        $n(22) attach $attack_dst($i) 7
    }
    if { $i >= 10 && $i < 15 } {
        $n(24) attach $attack_dst($i) 7
    }
    if { $i >= 15 && $i < 20 } {
        $n(27) attach $attack_dst($i) 7
    }
    if { $i >= 20 && $i < 25 } {
        $n(28) attach $attack_dst($i) 7
    }
    if { $i >= 25 && $i < 30 } {
        $n(29) attach $attack_dst($i) 7
    }
    if { $i >= 30 && $i < 35 } {
        $n(38) attach $attack_dst($i) 7
    }
    if { $i >= 35 && $i < 40 } {
        $n(38) attach $attack_dst($i) 7
    }
    if { $i >= 40 && $i < 45 } {
        $n(33) attach $attack_dst($i) 7
    }
    if { $i >= 45 } {
        $n(34) attach $attack_dst($i) 7
    }
}

# Connect attackers to victims
for {set i 0} {$i < 50} {incr i} {
    if { $i < 10 } {
        $ns connect $attack_src($i) $attack_dst([expr 5 * $i])
    }
    if { $i >= 10 && $i < 20 } {
        $ns connect $attack_src($i) $attack_dst([expr 5 * [expr $i - 10] + 1])
    }
    if { $i >= 20 && $i < 30 } {
        $ns connect $attack_src($i) $attack_dst([expr 5 * [expr $i - 20] + 2])
    }
    if { $i >= 30 && $i < 40 } {
        $ns connect $attack_src($i) $attack_dst([expr 5 * [expr $i - 30] + 3])
    }
    if { $i >= 40 } {
        $ns connect $attack_src($i) $attack_dst([expr 5 * [expr $i - 40] + 4])
    }
}

# Setup CBR applications to simulate attacks over UDP connections
set nAttackers 50
for {set i 0} {$i < $nAttackers} {incr i} {
    set attacker($i) [new Application/Traffic/CBR]
    $attacker($i) set packetSize_ 200
    $attacker($i) set interval_ 0.05
    $attacker($i) attach-agent $attack_src($i)
}


proc attack {app start_time stop_time} {
    global ns
    $ns at $start_time "$app start"
    $ns at $stop_time "$app stop"
}

# To simulate attacks
for {set i 0} {$i < 50} {incr i} {
    if { $i < 10 } {
        attack $attacker($i) 0.7 9.9
    }
    if { $i >= 10 && $i < 20 } {
        attack $attacker($i) 1.2 9.2
    }
    if { $i >= 20 && $i < 30 } {
        attack $attacker($i) 0.8 8.0
    }
    if { $i >= 30 && $i < 40 } {
        attack $attacker($i) 1.5 9.5
    }
    if { $i >= 40 } {
        attack $attacker($i) 1.0 8.5
    }
}


#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < 180 } {incr i} {
    $ns at 10.000001 "$n($i) reset";
}

$ns at 10.000001 "puts \"Simulation ends.\""
$ns at 10.000002 "finish"

# Define a 'finish' procedure
proc finish {} {
        global ns file1 file2
        $ns flush-trace
        close $file1
        close $file2
        # exec nam network.attacks.nam &
        exit 0
}

puts "Starting simulation..."
$ns run

